<?php
include 'config/config.inc.php';
rinnovaSessione();
$_POST['op']!="" ?
$op = $_POST['op'] :
$op = $_GET['op'];
switch($op) {
	case "file_carte":
		$id = $_GET['id'];
		$content = '<form action="upload.php" method="POST" enctype="multipart/form-data">
			<div class="riga"></div>
			<div class="riga">
<div class="cell-label-form" style="width:270px;">Seleziona il file da importare in formato CSV:</div>
<div class="cell-input-form"><input type="file" id="file-csv" name="file-csv" tabindex="1" /></div>
<div class="chiudi"></div>
</div>
<div class="riga" style="text-align:center;margin-top:50px;">
<input type="hidden" id="op" name="op" value="importa_file_carte">
<input type="hidden" id="id" name="id" value="'.$id.'">
<input class="form-button" type="submit" value="CARICA" tabindex="2" />
</div>
		</form>';
		break;
		
	case "importa_file_carte":
		$file = $_FILES['file-csv'];
		$id = $_POST['id'];
		if($file['name']!="" && ($file['type']=="text/csv" || $file['type']=="text/comma-separated-values") ) {
			$filename = $_SERVER[DOCUMENT_ROOT]."/".FOLDER."/uploads/".$file['name'];
			if(move_uploaded_file($file['tmp_name'], $filename)) {
				if(($handle = fopen($filename, "r")) !== FALSE) {
					$conn->Execute("DELETE FROM carte_punto_vendita WHERE idpdv=$id;");
					$qry = "INSERT INTO carte_punto_vendita VALUES ";
					$row = 1;
					$end = true;
					while ((($data = fgetcsv($handle, 5000, ";")) !== FALSE ) && $end)  {
						if($row > 1) {	
							if($row % 1000 == 0) {
								$rs = $conn->Execute($qry);
								if($rs) {
									$row = 2;
									$qry = "INSERT INTO carte_punto_vendita VALUES ";
									$qry .= "(NULL,$id,'$data[1]','".addslashes($data[2])."','".addslashes($data[3])."',
									'".($data[4]!="" ? $data[4] : 0)."','".addslashes($data[5])."','".addslashes($data[6])."',
									".str_replace(",",".", $data[7]).",".str_replace(",",".", $data[8]).")";
								} else {
									$end = false;
								}
							} else {
								if($row > 2) $qry.=",";
								$qry .= "(NULL,$id,'$data[1]','".addslashes($data[2])."','".addslashes($data[3])."',
								'".($data[4]!="" ? $data[4] : 0)."','".addslashes($data[5])."','".addslashes($data[6])."',
								".str_replace(",",".", $data[7]).",".str_replace(",",".", $data[8]).")";
							}
						}
						$row++;
					}
					if($end) {
						if($qry != "INSERT INTO carte_punto_vendita VALUES ") {
							$rs = $conn->Execute($qry);
							if($rs)
							  $content = '<div class="riga">
							<div id="messaggio" class="form-box-messaggio">Importazione file avvenuta con successo!</div>
							</div>';
						    else
						      $content = '<div class="riga">
						    	<div id="messaggio" class="form-box-messaggio">Si &egrave; verificato un errore durante l\'upload del file, si prega di riprovare<br>'.$conn->ErrorMsg().'</div>
						    	<div class="riga" style="text-align:center;"><input type="button" class="form-button" value="RITORNA" onClick="location.href=\'upload.php?op=file_carte&id='.$id.'\'"></div>
						    	</div>';
						} else
						   $content = '<div class="riga">
						<div id="messaggio" class="form-box-messaggio">Importazione file avvenuta con successo!</div>
						</div>';
					} else {
						$content = '<div class="riga">
						<div id="messaggio" class="form-box-messaggio">Si &egrave; verificato un errore durante l\'upload del file, si prega di riprovare<br>'.$conn->ErrorMsg().'</div>
						<div class="riga" style="text-align:center;"><input type="button" class="form-button" value="RITORNA" onClick="location.href=\'upload.php?op=file_carte&id='.$id.'\'"></div>
						</div>';
					}
				}
				unlink($filename);
			} else 
				$content = '<div class="riga">
<div id="messaggio" class="form-box-messaggio">Si &egrave; verificato un errore durante l\'upload del file, si prega di riprovare</div>
<div class="riga" style="text-align:center;"><input type="button" class="form-button" value="RITORNA" onClick="location.href=\'upload.php?op=file_carte&id='.$id.'\'"></div>
</div>';
		} else {
			$content = '<div class="riga">
<div id="messaggio" class="form-box-messaggio">Devi inserire un file valido in formato CSV</div>
<div class="riga" style="text-align:center;"><input type="button" class="form-button" value="RITORNA" onClick="location.href=\'upload.php?op=file_carte&id='.$id.'\'"></div>
</div>';
		}
		break;

	default:
		break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;">
<title>Check Flayer Module</title>
<link href="stili/stili.css" rel="stylesheet" type="text/css" />
<link href="stili/jquery-ui.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" media="screen" type="text/css" href="js/jpicker/jPicker.css" />
<link rel="stylesheet" media="screen" type="text/css" href="js/jpicker/css/jPicker-1.1.6.min.css" />
<? include 'includes/js.php';?>
</head>
<body>
<?=$content?>
</body>
</html>